﻿using System.ComponentModel;

namespace MvvmUtils.ViewModels
{
    public abstract class ViewModel : INotifyPropertyChanged
    {
        private readonly Handler[] _handlers;

        public ViewModel(params Handler[] handlers)
        {
            this._handlers = handlers ?? new Handler[] { };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void Handle(Message message)
        {            
        }

        internal void OnPropertyChanged(IProperty changedProperty)
        {
            foreach (var handler in this._handlers)
            {
                handler.OnPropertyChanged(changedProperty);
            }

            this.RaisePropertyChanged(changedProperty.Name);
        }

        private void RaisePropertyChanged(string propertyName)
        {
            var h = this.PropertyChanged;
            if (h != null)
            {
                h(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
